﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class mystudy_video_title : System.Web.UI.Page
{
    public string title = string.Empty;
    public string jigou = string.Empty;
    public string laoshi = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        title = Session["skechengname"].ToString();
        jigou = Session["skechengjigou"].ToString();
        laoshi = Session["slaoshi"].ToString();
        Page.DataBind();
        if(!IsPostBack)
        {
            Li_Newcourse.Visible = false;
            Li_Check.Visible = false;
            Li_Access.Visible = false;
            Li_Pingfen.Visible = false;
            Li_Baoming.Visible = false;
            Unnamed1.Enabled = false;
        }
        bool z1 = (Session["sRole"].ToString() == "学员" | Session["sRole"].ToString() == "老师") & (Session["sKecheng"].ToString().IndexOf("," + Session["sid"].ToString() + ",") >= 0 | Session["sKechenged"].ToString().IndexOf("," + Session["sid"].ToString() + ",") >= 0); 
        bool z3 = Session["sRole"].ToString() == "机构" && Session["sJigou"].ToString() == Session["skechengjigou"].ToString(); 
        if (Session["sRole"].ToString() == "老师" & Session["slaoshi"].ToString() == Session["sUsername"].ToString())
        {
            z1 = true;
        }
        if (Session["sRole"].ToString() == "学员")
        {
            if (z1) //学员，且已经报名学习
            {
                if (Session["sKecheng_Voted"].ToString().IndexOf("," + Session["sid"] + ",") <= 0)    //还没有评分(本课程代码不在已投票课程序列中）
                {
                    DplPingfen.Enabled = true;
                    DplPingfen.Visible = true;
                    Li_Pingfen.Visible = true;
                    Li_Check.Visible = true;
                    Unnamed1.Text = "评分";
                    Unnamed1.Enabled = true;
                }

            }
            else    //学员，还没有报名学习课程，则显示“报名”按钮。//不显示 课程目录
            {
                Li_Baoming.Visible = true;
                BtnBaoming.Visible = true;
                BtnBaoming.Enabled = true;
            }

        }
        if (Session["sRole"].ToString() == "老师")
        {
            if (z1) //老师，且本课程是该老师教授
            {
                Li_Newcourse.Visible = true;
                Unnamed1.Text = "提交";
                if (Session["sconfirm"].ToString() == "审核未通过" | Session["sconfirm"].ToString() == "终审未通过" | Session["sconfirm"].ToString() == "新建")
                {
                    Li_Check.Visible = true;
                    Li_Check.Disabled = false;
                    Unnamed1.Enabled = true;
                }
            }
        }
        if (Session["sRole"].ToString() == "管理员")
        {
            Li_Newcourse.Visible = true;
            Li_Access.Visible = true;
            Li_Check.Visible = true;
            Li_Check.Disabled = true;
            Unnamed1.Text = "终审";
            if (Session["sconfirm"].ToString() == "审核通过")
            {
                Li_Check.Disabled = false;
                Unnamed1.Enabled = true;
                
            }
            if (Session["sconfirm"].ToString() == "提交")
            {
                Li_Check.Disabled = false;
                Unnamed1.Enabled = true;
            }
        } 
        if (z3)     //如果是机构管理员，且该课程属于该机构
        {
            Li_Newcourse.Visible = true;
            Li_Access.Visible = true;
            Li_Check.Visible = true;
            Li_Check.Disabled = true;
            Unnamed1.Text = "审核";
            if (Session["sconfirm"].ToString() == "提交")
            {
                Li_Check.Disabled = false;
                Unnamed1.Enabled = true;
            }
        }
        if (Session["sconfirm"].ToString() == "终审通过")//终审通过的课程不能再创建
        {
            Li_Newcourse.Visible = false;
            Unnamed1.Enabled = false;
        }
    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        string zfuckdo = Unnamed1.Text;
        switch (zfuckdo)
        {
            case "评分":
                zxdscore();
                break;
            case "提交":
                zxdconfirm0();
                break;
            case "审核":
                zxdconfirm1();
                break;
            case "终审":
                zxdconfirm2();
                break;
        }
    }
    protected void zxdscore()       //评分,评分之后因为记录Session[Svoted]
    {
        Conn zxdconn = new Conn();
        //Session[vote]+=,skechengid,
        Session["sKecheng_Voted"] = Session["sKecheng_Voted"].ToString().Trim() + "," + Session["sid"].ToString() + ",";
        //帐户.voted+=,skechengid,
        string zsql = "update account set voted='" + Session["sKecheng_Voted"].ToString() + "' where username='" + Session["sUsername"].ToString() + "'";
        string noth = zxdconn.ExeSql(zsql).ToString();
        //投票：insert into pingfen (课程，用户，分数，系统日期)values ()
        string zxtsj = DateTime.Now.ToString();
        zsql = "insert into pingfen(username,kecheng_id,kecheng,pingfen,pfsj) values('" + Session["sUsername"].ToString() + "'," + Session["sid"].ToString() + ",'" + Session["skechengname"].ToString() + "'," + DplPingfen.SelectedValue.ToString().Trim() + ",'" + zxtsj + "')";
        noth = zxdconn.ExeSql(zsql).ToString();
        //Kecheng.jifen+=积分
        zsql = "update kecheng set jifen=jifen+" + DplPingfen.SelectedValue.ToString().Trim() + " where id=" + Session["sid"].ToString();
        noth = zxdconn.ExeSql(zsql).ToString();
        //投票.enable=false
        //最后,还要将本页面刷新
        Response.Write("<script>parent.location.reload(true)</script>");

    }
    protected void zxdconfirm0()    //提交,提交之
    {
        Conn zconn = new Conn();
        //老师能够提交课程的前提,是考试题目至少有3套题，每套题的分数总额都必须是100分。
        string zsql = "select count(name) from tb_TaoTi where lessonid=" + Session["sid"].ToString();
        string ztaotinum = zconn.sqlFirst(zsql);
        //以下检查考试套题
        if (Int32.Parse(ztaotinum) < 3)
        {

            string msg = "<script language=javascript>alert('您的课程无法提交,请至少准备3套试题,以便学员完成学习后进行考核,按任意键返回.');if(opener!=null){opener.location.reload();window.close();}</script>";
            ClientScript.RegisterStartupScript(this.GetType(), "msg", msg, false);
            return;
        }
        
        //套题检查完毕
        //以下检查每套题总分
        zsql = "select sum(score) from tb_Questions where que_lessonid=" + Session["sid"].ToString() + " group by que_taotiid";
        int result = 0;//后面判断result如果<3则不满足条件无法提交
        SqlDataReader reader1 = zconn.Reader(zsql);
        while (reader1.Read())
        {
            int ztotalscore = reader1.GetInt32(0);
            if (ztotalscore != 100)
            {
                result++;
                string msg = "<script language=javascript>alert('每套考题的总分都必须是100分,请调整您的题目分数.');if(opener!=null){opener.location.reload();window.close();}</script>";
                ClientScript.RegisterStartupScript(this.GetType(), "msg", msg, false);
                return;
            }
        }
        reader1.Dispose();
        zconn.Dispose();
        //检查套题总分完毕


        string zfuckdo;
        string zxtsj = DateTime.Now.ToString();
        zfuckdo = "提交";
        Session["sconfirm"] = zfuckdo;
        zsql = "update kecheng set confirm='" + zfuckdo + "',xtsj2='" + zxtsj + "' where id=" + Session["sid"].ToString();
        zconn = new Conn();

        string noth = zconn.ExeSql(zsql).ToString();
        zconn.Dispose();
        //最后,还要将本页面刷新
        Response.Write("<script>parent.location.reload(true)</script>");
    }

    protected void zxdconfirm1()    //审核
    {
        Conn zxdconn = new Conn();
        string zfuckdo;
        string zxtsj = DateTime.Now.ToString();
        if (DropDownList1.SelectedValue.ToString() == "通过")
        {
            zfuckdo = "审核通过";
        }
        else
        {
            zfuckdo = "审核未通过";
        }
        Session["sconfirm"] = zfuckdo;
        string zsql = "update kecheng set confirm='" + zfuckdo + "',xtsj2='" + zxtsj + "' where id=" + Session["sid"].ToString();
        string noth = zxdconn.ExeSql(zsql).ToString();
        zxdconn.Dispose();
        //最后,还要将本页面刷新
        Response.Write("<script>parent.location.reload(true)</script>");

    }
    protected void zxdconfirm2()    //终审
    {
        Conn zxdconn = new Conn();
        string zfuckdo;
        string zstsj = DateTime.Now.ToString();
        if (DropDownList1.SelectedValue.ToString() == "通过")
        {
            zfuckdo = "终审通过";
        }
        else
        {
            zfuckdo = "终审未通过";
        }
        string zxtsj = DateTime.Now.ToString();
        Session["sconfirm"] = zfuckdo;
        string zsql = "update kecheng set confirm='" + zfuckdo + "',xtsj2='" + zxtsj + "' where id=" + Session["sid"].ToString();
        string noth = zxdconn.ExeSql(zsql).ToString();
        zxdconn.Dispose();
        //最后,还要将本页面刷新
        Response.Write("<script>parent.location.reload(true)</script>");
    }

    protected void BtnBaoming_Click(object sender, EventArgs e)
    {
        if ((Session["sKecheng"].ToString().IndexOf("," + Session["sid"].ToString() + ",") >= 0) | (Session["sKechenged"].ToString().IndexOf("," + Session["sid"].ToString() + ",") >= 0))   //尽管我已经作过了约束，报名的学员看不到"报名"按钮,但为了保险还是加入此判断
        {

            string msg = "<script language=javascript>alert('不用重复报名您已经报名参加了该课程，不用重复报名');if(opener!=null){opener.location.reload();window.close();}</script>";
            ClientScript.RegisterStartupScript(this.GetType(), "msg", msg, false);
        }
        else       //这里才是报名的代码
        {
            Conn zconn = new Conn();
            //首先应该判断：如果学生储值金额小于学费，则提示不能报名
            string zchuzhisql = "select chuzhi from account where username='" + Session["sUsername"].ToString() + "'";
            string zchuzhi = zconn.sqlFirst(zchuzhisql);
            string zxuefeisql = "select xuefei from kecheng where id=" + Session["sid"].ToString();
            string zxuefei1 = zconn.sqlFirst(zxuefeisql);

            if (float.Parse(zchuzhi) < float.Parse(zxuefei1))
            {
                zconn.Dispose();
                Response.Write("<script language=javascript>alert('您的储值金额不足，请充值后再报名。。。。。');if(opener!=null){opener.location.reload();window.close();}</script>");
                return;
            }
            //流程:
            //插入消费记录
            //session[“sKecheng”]+=, kecheng.id,
            //account.kecheng= session[“sKecheng”]。Tostring()
            //Account.已收课费+=已收课费
            //Kecheng.xuesheng=,xuesheng_id,
            //response.redirect(kecheng_play_temp.aspx

            string zxtsj = DateTime.Now.ToString();
            //首先将session变量更新
            Session["sKecheng"] = Session["sKecheng"].ToString() + "," + Session["sid"].ToString() + ",";

            //取得本课程的课程费用
            string zsql = "";
            zsql = "select xuefei from kecheng where id=" + Session["sid"].ToString();
            string zxuefei = zconn.sqlFirst(zsql).ToString().Trim();


            //修改本用户帐户,加入课程代码,
            zsql = "update account set [kecheng]='" + Session["sKecheng"].ToString() + "',[chuzhi]=[chuzhi]-" + zxuefei + ",[yiskf]=[yiskf]+" + zxuefei + " where [username]='" + Session["sUsername"].ToString() + "'";
            zconn.ExeSql(zsql);
            //插入消费记录-用户名称,消费对象,课程名称,消费金额,系统时间.
            //2008-03-03将该学生的ID加入老师的kecheng字段中

            //修改kecheng表,将本学生的id记入kecheng.xuesheng字段中
            //首先取得kecheng.xuesheng的值,加当前学生用户名后再赋值给kecheng.xuesheng,因为nText型字段不支持set [xuesheng]=[xuesheng]+的形式
            zsql = "select xuesheng from kecheng where [id]=" + Session["sid"].ToString();
            string zxueshenginkecheng = zconn.sqlFirst(zsql);
            if (zxueshenginkecheng == "0")
            {
                zxueshenginkecheng = "";
            }
            zxueshenginkecheng = zxueshenginkecheng + "," + Session["sUserName"].ToString() + ",";
            zsql = "update kecheng set [xuesheng]='" + zxueshenginkecheng + "' where [id]=" + Session["sid"].ToString();
            int noth = zconn.ExeSql(zsql);
            //修改完毕.zxd,2008-5-27

            zsql = "select [kecheng] from account where username='" + Session["slaoshi"].ToString() + "'";
            string zxuesheng = zconn.sqlFirst(zsql);
            if (zxuesheng.IndexOf("," + Session["sUsername"].ToString().Trim() + ",") == 0)//首先判断该学生是否在别的课程中选修过该老师的课程,如果没有,把他加入
            {
                zxuesheng += "," + Session["sUserName"].ToString() + ",";
                zsql = "update account set [kecheng]='" + zxuesheng + "' where username='" + Session["slaoshi"].ToString() + "'";
                zconn.ExeSql(zsql);
            }
            //加入完毕
            zsql = "insert into xiaofei (username,topic,topic1,xfje,xfsj) values('" + Session["sUsername"].ToString() + "','课程报名','" + Session["skechengname"].ToString() + "'," + zxuefei + ",'" + zxtsj + "')";
            int nothresult = zconn.ExeSql(zsql);
            zconn.Dispose();
            //最后,还要将本页面刷新
            Response.Write("<script>parent.location.reload(true)</script>");
        }
    }

    protected void CloseWindow_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.parent.close()</script>");
    }
}
